/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.Event;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import manager.EventManager;
import properties.PropertyHolder;

/**
 *
 * @author Stefan
 */
public class ImageServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Image Servlet Called");
        response.setContentType("image");
        EventManager evm = new EventManager();

        String path = getServletContext().getRealPath("");


        Long eventId;
        String[] url = request.getRequestURI().split("/");
        try {
            eventId = Long.parseLong(url[url.length - 1]);
        } catch (Exception e) {
            response.sendError(404);
            return;
        }
        try {
            String repositoryPath = PropertyHolder.getProperty("IMAGE_PATH");
            File file = new File(path + File.separatorChar + "static" + File.separatorChar + "images" + File.separatorChar + "NoImage.jpg");
            System.out.println("Event ID: " + eventId);
            Event event = evm.findByID(eventId);

            if (event.getImageName() != null) {
                File tempFile = new File(repositoryPath, "event" + File.separatorChar + event.getId() + File.separatorChar + event.getImageName());
                System.out.println(tempFile.getAbsolutePath());
                if (file.exists()) {
                    file = tempFile;
                }
            }

            InputStream is = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
            int c;
            while ((c = is.read()) != -1) {
                os.write(c);
            }
            os.flush();
            is.close();
            os.close();


        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(404);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
